import BButton from "../btoolkit/components/BButton";
import WeiXinUtils from "../btoolkit/sdk/weixin/WeiXinUtils";
import FriendlyLinkBar from "./FriendlyLinkBar";
import Popup from "../btoolkit/components/Popup";
import SDKManager, { Platform } from "../btoolkit/sdk/SDKManager";
import App from "../btoolkit/framework/App";
import QuLe from "../btoolkit/sdk/QuLe";

const { ccclass, property } = cc._decorator;

@ccclass
export default class FriendlyLinkDialog extends Popup {

    @property(cc.Node)
    item: cc.Node = null;

    static instance: cc.Node = null;

    static show() {
        if (FriendlyLinkDialog.instance) {
            return;
        }
        if (!QuLe.instance.remoteConfig) {
            console.error('远程配置文件未准备好');
            return;
        }
        FriendlyLinkDialog.instance = cc.instantiate(App.instance.resPreloader.getRes('prefabs/common/FriendlyLinkDialog'));
        FriendlyLinkDialog.instance.parent = cc.Canvas.instance.node;
    }

    onDestroy() {
        FriendlyLinkDialog.instance = null;
    }

    onLoad() {
        super.onLoad();
        this.item.active = false;
        let showGames = FriendlyLinkBar.instance.getShowGames();
        let showGamesCount = showGames.length;
        for (let i = 0; i < showGamesCount; i++) {
            let itemNode: cc.Node = null;
            if (i == 0) {
                itemNode = this.item;
            } else {
                itemNode = cc.instantiate(this.item);
                itemNode.parent = this.item.parent;
            }
            itemNode.active = true;
            let data = showGames[i];
            itemNode.getChildByName('icon').getComponent(cc.Sprite).spriteFrame = FriendlyLinkBar.instance.atlas.getSpriteFrame(data.frame);
            BButton.addButton(itemNode).setTouchUpEvent(() => { SDKManager.instance.navigateToOtherGame(data) });
        }
    }

}
